package xyz.labmem.main.modular.app.gmjj.controller

import com.alibaba.excel.EasyExcel
import xyz.labmem.main.modular.common.service.selectFormPage
import io.swagger.annotations.Api
import io.swagger.annotations.ApiOperation
import org.springframework.validation.annotation.Validated
import org.springframework.web.bind.annotation.*
import org.springframework.web.multipart.MultipartFile
import xyz.labmem.base.core.annotion.BusinessLog
import xyz.labmem.base.core.tool.isNotBlank
import xyz.labmem.main.modular.common.pojo.FormPage
import xyz.labmem.base.core.base.BaseVerify
import xyz.labmem.base.core.common.request.ComDelBatchReq
import xyz.labmem.base.core.common.response.ComAndColumnRes
import xyz.labmem.base.core.common.response.ComPageAndColumnRes
import xyz.labmem.base.core.common.response.ResData
import xyz.labmem.base.core.enums.LogAnnotationOpTypeEnum
import xyz.labmem.base.core.result.RP
import xyz.labmem.base.core.result.RV
import xyz.labmem.base.sys.base.BaseController
import xyz.labmem.base.sys.consts.PathPrefixConst
import xyz.labmem.main.modular.app.gmjj.entity.App_yjgljyjbzzdwzcbwzcjb
import xyz.labmem.main.modular.app.gmjj.mapper.App_yjgljyjbzzdwzcbwzcjb_Mapper
import xyz.labmem.main.modular.common.pojo.FormMap
import xyz.labmem.main.modular.common.service.appDownloadTemplate
import xyz.labmem.main.modular.common.service.AppTableExcelListener
import javax.servlet.http.HttpServletResponse

/**
 * @Author lty
 * @Date 2022/1/21 14:22
 */
@RestController
@Api(tags = ["应急保障重点物资（储备物资）"])
@RequestMapping("${PathPrefixConst.APP_PATH_PREFIX}yjgljyjbzzdwzcbwzcjb")
class App_yjgljyjbzzdwzcbwzcjb_Controller(
    private var appYjgljyjbzzdwzcbwzcjbMapper:  App_yjgljyjbzzdwzcbwzcjb_Mapper
) : BaseController() {

    @ApiOperation(value = "获取详情")
    @GetMapping(value = ["/{id}"])
    fun getInfo(@PathVariable("id") id: String): RV<*> {
        return success(ResData<App_yjgljyjbzzdwzcbwzcjb>().apply {
            data = appYjgljyjbzzdwzcbwzcjbMapper.selectById(id)
            list = getFields2(App_yjgljyjbzzdwzcbwzcjb::class)
        })
    }

    @PostMapping("mapDots")
    @ApiOperation(value = "地图打点应急保障重点物资")
    fun mapDots(@RequestBody @Validated(BaseVerify.page::class) req: FormMap): RP<ComAndColumnRes<App_yjgljyjbzzdwzcbwzcjb>> {
        val pages =appYjgljyjbzzdwzcbwzcjbMapper.selectList(selectFormPage<App_yjgljyjbzzdwzcbwzcjb>(req.query))
        return pageWithColumn(pages, App_yjgljyjbzzdwzcbwzcjb::class.java)
    }


    @PostMapping("page")
    @ApiOperation(value = "分页展示应急保障重点物资（储备物资）")
    fun page(@RequestBody @Validated(BaseVerify.page::class) req: FormPage): RP<ComPageAndColumnRes<App_yjgljyjbzzdwzcbwzcjb>> {
        val pages = appYjgljyjbzzdwzcbwzcjbMapper.selectPage(
            req.IPage(),
            selectFormPage<App_yjgljyjbzzdwzcbwzcjb>(req.query)
        )
        return pageWithColumn(pages, App_yjgljyjbzzdwzcbwzcjb::class.java)
    }

    @GetMapping("getFields")
    @ApiOperation(value = "获取所有字段应急保障重点物资（储备物资）")
    fun getFields(): RV<ArrayList<xyz.labmem.base.core.common.response.PageColumn>> {
        return success(getFields(App_yjgljyjbzzdwzcbwzcjb::class))
    }
    @BusinessLog(title = "修改数据应急保障重点物资", opType = LogAnnotationOpTypeEnum.UPDATE)
    @PostMapping("update")
    @ApiOperation(value = "修改数据应急保障重点物资（储备物资）")
    fun update(@RequestBody req: App_yjgljyjbzzdwzcbwzcjb): RV<*> {
        return if (req.id.isNotBlank()) {
            resultJudgment(req.updateById())
        } else
            failedMsg("id不能为空")
    }

    @DeleteMapping("delBatch")
    @ApiOperation(value = "批量删除应急保障重点物资（储备物资）")
    @BusinessLog(title = "批量删除应急保障重点物资", opType = LogAnnotationOpTypeEnum.DELETE)
    fun delBatch(@RequestBody @Validated req: ComDelBatchReq): RV<*> {
        return resultJudgment(appYjgljyjbzzdwzcbwzcjbMapper.deleteBatchIds(req.ids), "删除成功！", "没有数据被删除")
    }

    @BusinessLog(title = "导入应急保障重点物资（储备物资）", opType = LogAnnotationOpTypeEnum.IMPORT)
    @PostMapping("importData")
    @ApiOperation(value = "导入数据,“excel格式按照模板填写!”")
    fun importData(@RequestPart("file") file: MultipartFile): RV<*> {
        EasyExcel.read(
            file.inputStream,
            App_yjgljyjbzzdwzcbwzcjb::class.java,
            AppTableExcelListener(appYjgljyjbzzdwzcbwzcjbMapper)
        ).sheet().doRead()
        return success()
    }

    @GetMapping("downloadTemplate")
    @ApiOperation(value = "下载导入模板")
    fun downloadTemplate(response: HttpServletResponse) {
        appDownloadTemplate(this, response, App_yjgljyjbzzdwzcbwzcjb(),appYjgljyjbzzdwzcbwzcjbMapper)
    }

}